package com.linsen.core.common.util;

import lombok.experimental.UtilityClass;

import java.util.Random;

/** 随机数工具类 */
@UtilityClass
public final class RandomUtil {
    /**
     * 数字范围内的随机数
     * @param length
     * @return
     */
    public String randomNumber(int length){
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

    /**
     * 随机生成n位字母
     * @param length
     * @return
     */
    public String randomLetter(int length)
    {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int randomMax = str.length();
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(randomMax);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 数字/大写字母/小写字母范围内的随机数
     * @param length
     * @return
     */
    public String randomNumberLetter(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        int randomMax = str.length();
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(randomMax);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 输出指定长度的字符串格式的数字（主要是输出类似于“0001”的数据）
     */
    public String strNumber(int number,int length){
        StringBuilder str = new StringBuilder(String.valueOf(number));
        int strLength = str.length();
        if (strLength>=length){
            return str.toString();
        }
        for (int i = 0;i<length-strLength;i++){
            str.insert(0, "0");
        }

        return str.toString();
    }

    /**
     * 获得 区间随机数
     * @param min 最小值
     * @param max 最大值
     * @return
     */
    public Integer getRangeRandom(int min, int max) {
        Random random = new Random();
        return min + random.nextInt(max - min + 1);
    }
}
